#include "devicedatabaseoperations.h"
#include <QDebug>
#include "common.h"
#include "tabledefine.h"

QSqlDatabase deviceDB;

bool connectDeviceDB(QString dbName)
{
    if (QSqlDatabase::contains(DEVICE_CONNECTION))
    {
        deviceDB = QSqlDatabase::database(DEVICE_CONNECTION);
    }
    else
    {
        deviceDB = QSqlDatabase::addDatabase("QSQLITE", DEVICE_CONNECTION);
        deviceDB.setDatabaseName(dbName);
        deviceDB.setUserName("yanshang");
        deviceDB.setPassword("912");
    }
    if(!deviceDB.open())
    {
        qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<deviceDB.lastError();
        return false;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"连接并打开设备数据库成功";
    return true;
}

void closeDeviceDB()
{
    if(!deviceDB.isOpen())
        deviceDB.close();
}

void removeDeviceDB()
{
    if (QSqlDatabase::contains(DEVICE_CONNECTION))
        QSqlDatabase::removeDatabase(DEVICE_CONNECTION);
}

bool updateAllTableProductIDAndDeviceID(int productID, int deviceID)
{
    QSqlQuery query(deviceDB);
    
    //更新设备表产品id和设备id
    QString sql = QString("update %1 set %2=%3,%4=%5").arg(DEVICE_TABLE_NAME, PRODUCT_ID, _PRODUCT_ID, DEVICE_ID, _DEVICE_ID);
    query.prepare(sql);
    query.bindValue(_PRODUCT_ID, productID);
    query.bindValue(_DEVICE_ID, deviceID);
    if(!query.exec())
    {
        qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<query.lastError();
        return false;
    }

    //更新接口对象表产品id和设备id
    sql = QString("update %1 set %2=%3,%4=%5").arg(INTERFACE_OBJECT_TABLE_NAME, PRODUCT_ID, _PRODUCT_ID, DEVICE_ID, _DEVICE_ID);
    query.prepare(sql);
    query.bindValue(_PRODUCT_ID, productID);
    query.bindValue(_DEVICE_ID, deviceID);
    if(!query.exec())
    {
        qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<query.lastError();
        return false;
    }
    
    //更新通讯对象表产品id和设备id
    sql = QString("update %1 set %2=%3,%4=%5").arg(COMM_OBJECT_TABLE_NAME, PRODUCT_ID, _PRODUCT_ID, DEVICE_ID, _DEVICE_ID);
    query.prepare(sql);
    query.bindValue(_PRODUCT_ID, productID);
    query.bindValue(_DEVICE_ID, deviceID);
    if(!query.exec())
    {
        qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<query.lastError();
        return false;
    }
    
    //更新规则表产品id和设备id
    sql = QString("update %1 set %2=%3,%4=%5").arg(INTERFACE_COMM_OBJECT_RULE_TABLE_NAME, PRODUCT_ID, _PRODUCT_ID, DEVICE_ID, _DEVICE_ID);
    query.prepare(sql);
    query.bindValue(_PRODUCT_ID, productID);
    query.bindValue(_DEVICE_ID, deviceID);
    if(!query.exec())
    {
        qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<query.lastError();
        return false;
    }
    
    //更新地址表产品id和设备id
    sql = QString("update %1 set %2=%3,%4=%5").arg(ADDRESS_TABLE_NAME, PRODUCT_ID, _PRODUCT_ID, DEVICE_ID, _DEVICE_ID);
    query.prepare(sql);
    query.bindValue(_PRODUCT_ID, productID);
    query.bindValue(_DEVICE_ID, deviceID);
    if(!query.exec())
    {
        qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<query.lastError();
        return false;
    }
    
    //更新关联表产品id和设备id
    sql = QString("update %1 set %2=%3,%4=%5").arg(RELATION_TABLE_NAME, PRODUCT_ID, _PRODUCT_ID, DEVICE_ID, _DEVICE_ID);
    query.prepare(sql);
    query.bindValue(_PRODUCT_ID, productID);
    query.bindValue(_DEVICE_ID, deviceID);
    if(!query.exec())
    {
        qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<query.lastError();
        return false;
    }
    return true;
}

QString selectDeviceName()
{
    QSqlQuery query(deviceDB);
    QString sql = QString("select %1 from %2").arg(NAME, DEVICE_TABLE_NAME);
    if(!query.exec(sql))
    {
        qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<query.lastError();
        return NULL;
    }
    if(query.first())
    {
        qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取设备名成功";
        return query.value(0).toString();
    }
    return NULL;
}
